package com.zy.yun.controller.common;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.IService;
import com.github.pagehelper.PageHelper;
import com.zy.yun.base.annotation.NotNullPara;
import com.zy.yun.base.common.DataTable;
import com.zy.yun.base.common.PageBean;
import com.zy.yun.base.common.RestResult;
import com.zy.yun.base.common.VO;
import com.zy.yun.base.constant.StatusEnum;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: wangyanwei
 * @Date: 2019/9/24 08:47
 * @Description:
 */
public class BaseController<T, V> {

    @Autowired
    private IService<T> service;

    @Autowired
    private VO<V> vo;

    /**
     * 功能描述: 根据id查询
     *
     * @param id: 记录id
     * @auther: wangyanwei
     * @date: 2019/9/24 9:25
     * @return:
     */
    @NotNullPara({"id"})
    @GetMapping("getById")
    @ApiOperation(value = "根据id查询")
    @ApiImplicitParam(name = "id", value = "记录id")
    public RestResult getById(@RequestParam(value = "id") Serializable id) {
        return RestResult.buildSuccess( service.selectById(id));
    }


    /**
     * 功能描述: 修改
     *
     * @auther: wangyanwei
     * @date: 2019/9/24 9:55
     * @param:
     * @return:
     */
    @PostMapping("updateById")
    @ApiOperation(value = "根据id修改")
    public RestResult updateById(T t) {
        if (service.updateById(t)) {
            return RestResult.buildSuccess();
        }
        return RestResult.buildError();
    }

    /**
     * @param pageNum:      页数
     * @param pageSize      : 行数
     * @param queryWrapper: 过滤条件
     * @Author 王彦委
     * @Description: 分页查询
     * @Date: 22:23 2019/10/16
     */
    protected DataTable findPage(EntityWrapper<T> queryWrapper, @RequestParam(defaultValue = "30") Integer pageNum, @RequestParam(defaultValue = "0") Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize, true);
        List<T> list = service.selectList(queryWrapper);
        ArrayList<VO> vList = new ArrayList<>();
        VO<V> vvo = null;
        try {
            //把po转换成vo
            for (T t : list) {
                vvo=vo.getClass().newInstance();
                BeanUtils.copyProperties(t, vvo);
                vList.add(vvo);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        PageBean<VO> pageBean = new PageBean<VO>(vList);

        return new DataTable(pageBean);

    }

    /**
     * @Author: 王彦委
     * @Description: 根据id, 批量删除
     * @Date: 21:57 2019/10/19
     */
    @ApiOperation(value = "根据id,批量删除")
    @PostMapping("batchDelete")
    public RestResult batchDelete(@RequestParam(value = "ids") List<Long> ids) {

        if (CollectionUtils.isEmpty(ids)) {
            return RestResult.buildError("至少选择一条！");
        }

        if (service.deleteBatchIds(ids)) {
            return RestResult.buildSuccess();
        }
        return RestResult.buildError();

    }

    /**
     * 功能描述: 根据id批量修改状态
     *
     * @auther: wangyanwei
     * @date: 2019/9/30 10:05
     * @param:
     * @return:
     */
    @PostMapping("batchUpdateState")
    @ApiOperation(value = "根据id批量修改状态")
    public RestResult batchUpdateState(String status, T t, @RequestParam("ids") List<Long> ids) {

        if (CollectionUtils.isEmpty(ids)) {
            return RestResult.buildError("请选择要修改的记录！");
        }
        if (!StatusEnum.STATUS_STOP.getCode().equalsIgnoreCase(status) && !StatusEnum.STATUS_START.getCode().equalsIgnoreCase(status)) {
            return RestResult.buildError("状态有误！");
        }

        EntityWrapper<T> tQueryWrapper = new EntityWrapper<>();
        tQueryWrapper.in("id", ids);

        if (service.update(t, tQueryWrapper)) {
            return RestResult.buildSuccess();
        }
        return RestResult.buildError();
    }

}
